package com.sky.mapper;

import com.sky.entity.AddressBook;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface AddressBookMapper {

    /**
     * 查询当前用户的地址簿
     *
     * @param userId
     * @return
     */
    @Select("select * from address_book where user_id = #{userId} order by is_default desc, create_time desc")
    List<AddressBook> listByUserId(Long userId);

    /**
     * 新增地址
     *
     * @param addressBook
     */
    @Insert("insert into address_book(user_id, consignee, sex, phone, province_code, province_name, city_code, city_name, district_code, district_name, detail, label, create_time, is_default) " +
            "values(#{userId}, #{consignee}, #{sex}, #{phone}, #{provinceCode}, #{provinceName}, #{cityCode}, #{cityName}, #{districtCode}, #{districtName}, #{detail}, #{label}, #{createTime}, #{isDefault})")
    void insert(AddressBook addressBook);

    /**
     * 根据 id 查询地址信息
     *
     * @param id
     * @return
     */
    @Select("select * from address_book where id = #{id}")
    AddressBook getById(Long id);

    /**
     * 根据 id 修改地址
     *
     * @param addressBook
     */
    void update(AddressBook addressBook);

    /**
     * 删除地址
     *
     * @param id
     */
    @Delete("delete from address_book where id = #{id}")
    void delete(Long id);

    /**
     * 根据用户 id 设置其所有地址为非默认
     *
     * @param addressBook
     */
    @Update("update address_book set is_default = #{isDefault} where user_id = #{userId}")
    void updateDefaultByUserId(AddressBook addressBook);

    /**
     * 查询默认地址
     *
     * @param addressBook
     * @return
     */
    @Select("select * from address_book where user_id = #{userId} and is_default = #{isDefault}")
    AddressBook getDefault(AddressBook addressBook);
}
